﻿/*
   Move windows around a bit
   Created by Randall Maas, Tuesday October 20 2009
   Copyright (c) 2009, Randall Maas
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

   Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

   Neither the name of Blackwood Designs, LLC nor the names of its contributors
   may be used to endorse or promote products derived from this software without
   specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   THE POSSIBILITY OF SUCH DAMAGE.
*/
using System ;
using System . Drawing;
using System . Threading;
using System . Diagnostics;

class WinMass : IDisposable
{
   /// <summary>
   /// Extra bits for the position of a window
   /// </summary>
   double Px, Py;

   /// <summary>
   /// Mass of the window (for physics perspective)
   /// </summary>
   double Mass;

   /// <summary>
   /// Velocity of the window
   /// </summary>
   PointF Velocity;

   /// <summary>
   /// X
   /// </summary>
   double Viscosity = 10;


   /// <summary>
   /// The window handle
   /// </summary>
   internal IntPtr Wnd;


   /// <summary>
   /// The save place of the window
   /// </summary>
   WINDOWPLACEMENT Placement;

   /// <summary>
   /// The process associated with the window
   /// </summary>
   Process Process;
   internal int    Exited = 0;

   internal WinMass(IntPtr Wnd)
   {
      this . Wnd  = Wnd;
      Placement= new WINDOWPLACEMENT(Wnd);
      RECT R;
      Win32 . GetWindowRect(Wnd, out R);
      Mass = R . Width*R .Height/1024;
      if (Mass < 1)
        Mass = 1;
      
      // Get a process object to allow tracking of a process exit
      Process = Win32 . Process(Wnd);
      Process . Exited += delegate(object sender, EventArgs e)
         {
            Interlocked . Exchange(ref Exited, 1);
         };
      Viscosity = Program . Rnd . NextDouble()* 30 + 1;
   }


   ~WinMass()
   {
      Dispose();
   }

   public void Dispose()
   {
      GC . SuppressFinalize(this);
//      if (0 == Exited)
//        Placement . Apply(Wnd);
      Process . Dispose();
   }


   // 
   internal bool Step(PointF F, double deltaT, double Width, double Height)
   {
      // If the process has exited, remove
      if (0 != Exited)
        return false;

      RECT Rect;
      Win32 . GetWindowRect(Wnd, out Rect);
      if (Rect . Left < -1000 && Rect . Top < -1000)
        return false;

      double SizeX = Rect . Width;
      double SizeY = Rect . Height;
      double c = deltaT /Mass;
      double aX = (-Velocity . X *Velocity . X * Math . Sign(Velocity.X)* Viscosity + F.X) * c;
      double aY = (-Velocity . Y *Velocity . Y * Math . Sign(Velocity.Y)* Viscosity + F.Y) * c;
      double dX = (Velocity . X + 0.5*aX)* deltaT;
      double dY = (Velocity . Y + 0.5*aY)* deltaT;
      Px += dX;
      Py += dY;


      Velocity . X = (float)(Velocity . X + aX);
      Velocity . Y = (float)(Velocity . Y + aY);
      if (Double . IsNaN(Velocity.X))
        Velocity . X = 0;
      if (Double . IsNaN(Velocity.Y))
        Velocity . Y = 0;
      if ( (0 == (int) Px && 0 == (int) Py) || !  Win32 . IsVisible(Wnd))
        return false;
      Px += Rect . Left ;
      Py += Rect . Top  ;
      if (Px < 0)
        {
           Px = 0;
           if (Velocity . X < 0)
             Velocity . X = 0;
        }
      else if (Px + SizeX > Width)
        {
           Px = SizeX > Width ? 0 : Width - SizeX;
           if (Velocity . X > 0)
             Velocity . X = 0;
        }
      else if (Double . IsNaN(Px))
        Px = 0;
      if (Py < 0)
        {
           Py = 0;
           if (Velocity . Y < 0)
             Velocity . Y = 0;
        }
      else if (Py + SizeY > Height)
        {
           Py = Height - SizeY;
           if (Velocity . Y > 0)
             Velocity . Y = 0;
        }
      else if (Double . IsNaN(Py))
        Py = 0;


      // Update the position only if it has changed
      if (Rect . Left != (int) Px || Rect . Top != (int) Py)
        Win32 . Position(Wnd, IntPtr . Zero, (int) Px, (int) Py, SWP . NOZORDER);

      // Set the window position
      Px -= (int) Px;
      Py -= (int) Py;
      return true;
   }
}

